Fix: Orphaned Partial Ask Messages // Message Guard #3514
+463
−22
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Improved version of: #3468
Context
Fixed a race condition where checkpoint messages (checkpoint_saved) could arrive during partial message updates, causing the message history to break and leaving partial messages orphaned with partial=true state.
Root Cause:
The checkpoint service runs git operations asynchronously in the background. When a checkpoint completes, it calls task.say("checkpoint_saved", ...) through an event handler, which can execute while Task.ask() or Task.say() are still updating partial messages. This resulted in checkpoint messages being inserted between a partial message's start and completion, breaking the message chain.
Previous Workaround:
The findPartialAskMessage() and findPartialSayMessage() functions were added to search backwards through messages to locate orphaned partials, but this was a symptom of the underlying race condition.
Implementation
Implemented a Message Insertion Guard pattern that serializes all message insertions without blocking checkpoint git operations.
Program Flow
Message Insertion Sequence (with guard):
Checkpoint Flow (unchanged, still async):
How to Test
Prerequisites
Test Scenario 1: Verify No Orphaned Partial Messages
Expected: Message history remains in correct order with no partial=true messages left behind
How to verify:
Test Scenario 2: Verify Checkpoint Performance
Test Scenario 3: Run Automated Tests